export default {
	// 跳转
	navigate(url, data, name) {
		try {
			let token = uni.getStorageSync("token");
			if (!token) {
				this.clearInfo()
			} else {
				if (name && data) {
					uni.setStorageSync(name, data);
				}
				uni.navigateTo({
					url
				})
			}
		} catch (e) {
			uni.showToast({
				title: '跳转失败,请重试',
				icon: 'none',
				position: 'bottom'
			})
		}

	},
	redirect(url, data, name) {
		try {
			let token = uni.getStorageSync("token");
			if (!token) {
				this.clearInfo()
			} else {
				if (name && data) {
					uni.setStorageSync(name, data);
				}
				uni.redirectTo({
					url
				})
			}
		} catch (e) {
			uni.showToast({
				title: '跳转失败,请重试',
				icon: 'none',
				position: 'bottom'
			})
		}
	},
	// 字体图标
	fontFamily() {
		const domModule = uni.requireNativePlugin('dom')
		domModule.addRule('fontFace', {
			'fontFamily': "myIconfont",
			'src': "url('https://at.alicdn.com/t/c/font_3751909_4gyf467vqbt.ttf?t=1667788473557')"
		})
	},
	// 上传图片
	img(obj = {}) {
		return new Promise((resolve, reject) => {
			const type = obj.type || '';
			const success = obj.success;
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				success: (res) => {
					const filePath = res.tempFilePaths[0];
					if (type != "chat") {
						uni.showLoading({
							title: '上传中...',
							mask: true
						});
					};
					uni.uploadFile({
						url: 'http://119.45.46.157:10090/upload', //仅为示例，非真实的接口地址
						filePath,
						name: 'file',
						success: (uploadFileRes) => {
							uni.hideLoading()
							resolve(JSON.parse(uploadFileRes && uploadFileRes.data ||
								'{}'))
						}
					});
				},
				fail(err) {
					uni.hideLoading()
					uni.showToast({
						title: err.msg || err.error || err.errMsg || '上传失败',
						icon: "none",
						position: 'bottom'
					});
					reject(err)
				}
			})
		})

	},
	// 错误处理
	errorout(err) {
		let code = err.code;
		console.log(err.message)
		if (code == 403) {
			this.clearInfo()
		} else if (code == 500) {
			uni.showToast({
				title: '系统错误',
				icon: "none",
				position: 'bottom'
			});
		} else if (code == "PERMISSION_ERROR") {
			uni.showToast({
				title: '您暂无此权限',
				icon: "none",
				position: 'bottom'
			});
		} else if (code == "VALIDATION_ERROR") {
			uni.showToast({
				title: '格式错误',
				icon: "none",
				position: 'bottom'
			});
		} else if (code == "DUPLICATE_KEY") {
			uni.showToast({
				title: '出错了，请重试',
				icon: "none",
				position: 'bottom'
			});
		} else {
			uni.showToast({
				title: '网络好像开小差了！',
				icon: "none",
				position: 'bottom'
			});
		}
	},
	// tab点击声音
	tabmp3() {
		const innerAudioContext = uni.createInnerAudioContext();
		innerAudioContext.autoplay = true;
		innerAudioContext.src = '/static/audio/an.mp3';
		innerAudioContext.onPlay(() => {
			console.log('播放成功')
		});
		innerAudioContext.onError((res) => {
			console.log(res.errMsg);
			console.log(res.errCode);
		});
	},
	// 复制数据
	fz(data) {
		uni.setClipboardData({
			data,
			success: () => {
				uni.showToast({
					icon: "none",
					title: '复制成功',
					position: 'bottom'
				});
			}
		});
	},
	// 获取用户地址
	userAddress(obj) {
		const type = obj.type || '';
		const success = obj.success;
		uni.getLocation({
			type: 'wgs84',
			geocode: true,
			success: (res) => {
				console.log(res)
				success(res);
			}
		});
	},
	// 保存图片
	bctp(url) {
		uni.showLoading({
			title: '保存中',
			mask: true
		});
		uni.downloadFile({
			url,
			success: (res) => {
				uni.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: () => {
						uni.showToast({
							icon: 'none',
							title: '保存成功',
							position: 'bottom'
						})
					}
				});
			}
		});
	},
	// 访问链接
	fwlj(url) {
		plus.runtime.openURL(url, function() {
			uni.showToast({
				icon: 'none',
				title: '链接错误，访问失败',
				position: 'bottom'
			})
		})
	},
	fwlj2(url) {
		plus.runtime.openWeb(url, function() {
			uni.showToast({
				icon: 'none',
				title: '链接错误，访问失败',
				position: 'bottom'
			})
		})
	},
	// 放大图片
	fdImg(url) {
		let urls = []
		urls.push(url)
		uni.previewImage({
			urls
		})
	},
	// 清除
	clearInfo(i) {
		// uni.removeStorageSync('userinfo');
		uni.removeStorageSync('token');
		uni.removeStorageSync('bound');
		if (!i) {
			uni.showToast({
				title: '请先登录',
				icon: "none",
				position: 'bottom'
			});
			uni.switchTab({
				url: '/pages/tabBar/home/index'
			});
			// uni.redirectTo({
			// 	url: '../login/login'
			// })
		}
	},
	// 识别二维码
	sbCode(url) {
		uni.downloadFile({
			url,
			success: (res) => {
				console.log(res.tempFilePath)
				let tempFilePath = res.tempFilePath;
				plus.barcode.scan(tempFilePath, function(type, result) {
					plus.runtime.openWeb(result)
				}, function(e) {
					uni.showToast({
						title: '识别失败，请保存二维码，去别处看看',
						icon: 'none',
						position: 'bottom'
					})
				})
			}
		})
	},
	// 放大识别
	fdCode(url) {
		let urls = []
		urls.push(url)
		uni.previewImage({
			urls,
			longPressActions: {
				itemList: ['识别二维码'],
				success: (data) => {
					if (data.tapIndex == 0) {
						// 转为临时路径
						uni.downloadFile({
							url,
							success: (res) => {
								console.log(res.tempFilePath)
								let tempFilePath = res.tempFilePath;
								plus.barcode.scan(tempFilePath, function(type, result) {
									plus.runtime.openWeb(result)
								}, function(e) {
									uni.showToast({
										title: '识别失败，请保存二维码，去别处看看',
										icon: 'none',
										position: 'bottom'
									})
								})
							}
						})
					}
				}
			}
		})
	}
}
